package com.example.weather;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class MyDataHelper extends SQLiteOpenHelper {

    private static final String TABLE_NAME = "city";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_SC = "collect";

    public MyDataHelper(@Nullable Context context) {
        super(context, "cccity.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql= "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME + " TEXT, " + COLUMN_SC + " TEXT);";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    //添加
    public String addOne(cityModel item){
        ContentValues cv=new ContentValues();
        cv.put(COLUMN_NAME,item.getName());
        cv.put(COLUMN_SC,item.getCollect());

        SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
        long insert=sqLiteDatabase.insert(TABLE_NAME,COLUMN_NAME,cv);
        if(insert==-1){
            return "fail";
        }
        sqLiteDatabase.close();
        return "success";
    }

    //删除
    public String deleteOne(cityModel item){
        SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
        int delete=sqLiteDatabase.delete(TABLE_NAME,COLUMN_ID+"=?",new String[]{String.valueOf(item.getId())});
        sqLiteDatabase.close();
        if(delete==0){
            return "fail";
        }
        return "success";
    }

    //修改
    public String updateOne(cityModel item){
        ContentValues cv=new ContentValues();
        cv.put(COLUMN_NAME,item.getName());
        cv.put(COLUMN_NAME,item.getCollect());

        SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
        int update=sqLiteDatabase.update(TABLE_NAME,cv,COLUMN_ID+"=?",new String[]{String.valueOf(item.getId())});
        sqLiteDatabase.close();
        if(update==0){
            return "fail";
        }
        return "success";
    }

    //查
    public List<cityModel> getAll(){
        List<cityModel> list =new ArrayList<>();
        cityModel item;
        String sql="SELECT * FROM "+TABLE_NAME;
        SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
        Cursor cursor=sqLiteDatabase.rawQuery(sql,null);
        int idIndex=cursor.getColumnIndex(COLUMN_ID);
        int nameIndex=cursor.getColumnIndex(COLUMN_NAME);
        int collectIndex=cursor.getColumnIndex(COLUMN_SC);
        while (cursor.moveToNext()){
            item=new cityModel(cursor.getInt(idIndex),cursor.getString(nameIndex),cursor.getString(collectIndex));
            list.add(item);
        }
        sqLiteDatabase.close();

        return list;
    }
}
